package com.nh.actionTerminal;

import java.util.List;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.nh.entity.GKLClassType;

public class ClassTypeAction extends GKLTerminalBaseAction {
	private int typeId;

	public void classTypeListByType() {
		//List<GKLClassType> classTypeList = this.getBaseService().findByProperty("superTitle", getSuperTypeByType(typeId), GKLClassType.class);
		Criteria criteria = this.createCriteria(GKLClassType.class);
		// criteria.addOrder(Order.desc("publishTime"));
		criteria.addOrder(Order.asc("typeOrder"));
		criteria.add(Restrictions.eq("superTitle", getSuperTypeByType(typeId)));
		
		List<GKLClassType> classTypeList=criteria.list();
		
		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "beanState", "remark" });
		// JSONSerializer.toJSON(classTypeList, config).toString()
		writResponse("{\"count\":\"" + classTypeList.size() + "\",\"list\":" + JSONSerializer.toJSON(classTypeList, config).toString() + "}");
	}
	public void classTypeListAll() {
		List<GKLClassType> classTypeList1=this.createCriteria(GKLClassType.class).addOrder(Order.asc("typeOrder")).add(Restrictions.eq("superTitle", getSuperTypeByType(0))).list();
		List<GKLClassType> classTypeList2=this.createCriteria(GKLClassType.class).addOrder(Order.asc("typeOrder")).add(Restrictions.eq("superTitle", getSuperTypeByType(1))).list();
		List<GKLClassType> classTypeList3=this.createCriteria(GKLClassType.class).addOrder(Order.asc("typeOrder")).add(Restrictions.eq("superTitle", getSuperTypeByType(2))).list();
		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "beanState", "remark" });
		// JSONSerializer.toJSON(classTypeList, config).toString()
		writResponse("{\"list1\":" +  JSONSerializer.toJSON(classTypeList1, config).toString() + ",\"list2\":" +  JSONSerializer.toJSON(classTypeList2, config).toString() + ",\"list3\":" + JSONSerializer.toJSON(classTypeList3, config).toString() + "}");
	}

	public int getTypeId() {
		return typeId;
	}

	public void setTypeId(int typeId) {
		this.typeId = typeId;
	}

	public static String getSuperTypeByType(int type) {
		String temp = "";
		switch (type) {
		case 0:
			temp = "产品推荐";
			break;
		case 1:
			temp = "产品说明";
			break;
		case 2:
			temp = "更多帮助";
			break;

		default:
			break;
		}
		return temp;
	}
}
